<!doctype html>
<html>
<head>
<meta charset='UTF-8'><meta name='viewport' content='width=device-width initial-scale=1'>
<title>Node.js</title><style type='text/css'>html {overflow-x: initial !important;}:root { --bg-color:#ffffff; --text-color:#333333; --select-text-bg-color:#B5D6FC; --select-text-font-color:auto; --monospace:"Lucida Console",Consolas,"Courier",monospace; }
html { font-size: 14px; background-color: var(--bg-color); color: var(--text-color); font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; -webkit-font-smoothing: antialiased; }
body { margin: 0px; padding: 0px; height: auto; bottom: 0px; top: 0px; left: 0px; right: 0px; font-size: 1rem; line-height: 1.42857; overflow-x: hidden; background: inherit; tab-size: 4; }
iframe { margin: auto; }
a.url { word-break: break-all; }
a:active, a:hover { outline: 0px; }
.in-text-selection, ::selection { text-shadow: none; background: var(--select-text-bg-color); color: var(--select-text-font-color); }
#write { margin: 0px auto; height: auto; width: inherit; word-break: normal; overflow-wrap: break-word; position: relative; white-space: normal; overflow-x: visible; padding-top: 40px; }
#write.first-line-indent p { text-indent: 2em; }
#write.first-line-indent li p, #write.first-line-indent p * { text-indent: 0px; }
#write.first-line-indent li { margin-left: 2em; }
.for-image #write { padding-left: 8px; padding-right: 8px; }
body.typora-export { padding-left: 30px; padding-right: 30px; }
.typora-export .footnote-line, .typora-export li, .typora-export p { white-space: pre-wrap; }
.typora-export .task-list-item input { pointer-events: none; }
@media screen and (max-width: 500px) {
  body.typora-export { padding-left: 0px; padding-right: 0px; }
  #write { padding-left: 20px; padding-right: 20px; }
  .CodeMirror-sizer { margin-left: 0px !important; }
  .CodeMirror-gutters { display: none !important; }
}
#write li > figure:last-child { margin-bottom: 0.5rem; }
#write ol, #write ul { position: relative; }
img { max-width: 100%; vertical-align: middle; image-orientation: from-image; }
button, input, select, textarea { color: inherit; font: inherit; }
input[type="checkbox"], input[type="radio"] { line-height: normal; padding: 0px; }
*, ::after, ::before { box-sizing: border-box; }
#write h1, #write h2, #write h3, #write h4, #write h5, #write h6, #write p, #write pre { width: inherit; }
#write h1, #write h2, #write h3, #write h4, #write h5, #write h6, #write p { position: relative; }
p { line-height: inherit; }
h1, h2, h3, h4, h5, h6 { break-after: avoid-page; break-inside: avoid; orphans: 4; }
p { orphans: 4; }
h1 { font-size: 2rem; }
h2 { font-size: 1.8rem; }
h3 { font-size: 1.6rem; }
h4 { font-size: 1.4rem; }
h5 { font-size: 1.2rem; }
h6 { font-size: 1rem; }
.md-math-block, .md-rawblock, h1, h2, h3, h4, h5, h6, p { margin-top: 1rem; margin-bottom: 1rem; }
.hidden { display: none; }
.md-blockmeta { color: rgb(204, 204, 204); font-weight: 700; font-style: italic; }
a { cursor: pointer; }
sup.md-footnote { padding: 2px 4px; background-color: rgba(238, 238, 238, 0.7); color: rgb(85, 85, 85); border-radius: 4px; cursor: pointer; }
sup.md-footnote a, sup.md-footnote a:hover { color: inherit; text-transform: inherit; text-decoration: inherit; }
#write input[type="checkbox"] { cursor: pointer; width: inherit; height: inherit; }
figure { overflow-x: auto; margin: 1.2em 0px; max-width: calc(100% + 16px); padding: 0px; }
figure > table { margin: 0px; }
tr { break-inside: avoid; break-after: auto; }
thead { display: table-header-group; }
table { border-collapse: collapse; border-spacing: 0px; width: 100%; overflow: auto; break-inside: auto; text-align: left; }
table.md-table td { min-width: 32px; }
.CodeMirror-gutters { border-right: 0px; background-color: inherit; }
.CodeMirror-linenumber { user-select: none; }
.CodeMirror { text-align: left; }
.CodeMirror-placeholder { opacity: 0.3; }
.CodeMirror pre { padding: 0px 4px; }
.CodeMirror-lines { padding: 0px; }
div.hr:focus { cursor: none; }
#write pre { white-space: pre-wrap; }
#write.fences-no-line-wrapping pre { white-space: pre; }
#write pre.ty-contain-cm { white-space: normal; }
.CodeMirror-gutters { margin-right: 4px; }
.md-fences { font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; overflow: visible; white-space: pre; background: inherit; position: relative !important; }
.md-diagram-panel { width: 100%; margin-top: 10px; text-align: center; padding-top: 0px; padding-bottom: 8px; overflow-x: auto; }
#write .md-fences.mock-cm { white-space: pre-wrap; }
.md-fences.md-fences-with-lineno { padding-left: 0px; }
#write.fences-no-line-wrapping .md-fences.mock-cm { white-space: pre; overflow-x: auto; }
.md-fences.mock-cm.md-fences-with-lineno { padding-left: 8px; }
.CodeMirror-line, twitterwidget { break-inside: avoid; }
.footnotes { opacity: 0.8; font-size: 0.9rem; margin-top: 1em; margin-bottom: 1em; }
.footnotes + .footnotes { margin-top: 0px; }
.md-reset { margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: top; background: 0px 0px; text-decoration: none; text-shadow: none; float: none; position: static; width: auto; height: auto; white-space: nowrap; cursor: inherit; -webkit-tap-highlight-color: transparent; line-height: normal; font-weight: 400; text-align: left; box-sizing: content-box; direction: ltr; }
li div { padding-top: 0px; }
blockquote { margin: 1rem 0px; }
li .mathjax-block, li p { margin: 0.5rem 0px; }
li { margin: 0px; position: relative; }
blockquote > :last-child { margin-bottom: 0px; }
blockquote > :first-child, li > :first-child { margin-top: 0px; }
.footnotes-area { color: rgb(136, 136, 136); margin-top: 0.714rem; padding-bottom: 0.143rem; white-space: normal; }
#write .footnote-line { white-space: pre-wrap; }
@media print {
  body, html { border: 1px solid transparent; height: 99%; break-after: avoid; break-before: avoid; font-variant-ligatures: no-common-ligatures; }
  #write { margin-top: 0px; padding-top: 0px; border-color: transparent !important; }
  .typora-export * { -webkit-print-color-adjust: exact; }
  html.blink-to-pdf { font-size: 13px; }
  .typora-export #write { break-after: avoid; }
  .typora-export #write::after { height: 0px; }
  .is-mac table { break-inside: avoid; }
}
.footnote-line { margin-top: 0.714em; font-size: 0.7em; }
a img, img a { cursor: pointer; }
pre.md-meta-block { font-size: 0.8rem; min-height: 0.8rem; white-space: pre-wrap; background: rgb(204, 204, 204); display: block; overflow-x: hidden; }
p > .md-image:only-child:not(.md-img-error) img, p > img:only-child { display: block; margin: auto; }
#write.first-line-indent p > .md-image:only-child:not(.md-img-error) img { left: -2em; position: relative; }
p > .md-image:only-child { display: inline-block; width: 100%; }
#write .MathJax_Display { margin: 0.8em 0px 0px; }
.md-math-block { width: 100%; }
.md-math-block:not(:empty)::after { display: none; }
[contenteditable="true"]:active, [contenteditable="true"]:focus, [contenteditable="false"]:active, [contenteditable="false"]:focus { outline: 0px; box-shadow: none; }
.md-task-list-item { position: relative; list-style-type: none; }
.task-list-item.md-task-list-item { padding-left: 0px; }
.md-task-list-item > input { position: absolute; top: 0px; left: 0px; margin-left: -1.2em; margin-top: calc(1em - 10px); border: none; }
.math { font-size: 1rem; }
.md-toc { min-height: 3.58rem; position: relative; font-size: 0.9rem; border-radius: 10px; }
.md-toc-content { position: relative; margin-left: 0px; }
.md-toc-content::after, .md-toc::after { display: none; }
.md-toc-item { display: block; color: rgb(65, 131, 196); }
.md-toc-item a { text-decoration: none; }
.md-toc-inner:hover { text-decoration: underline; }
.md-toc-inner { display: inline-block; cursor: pointer; }
.md-toc-h1 .md-toc-inner { margin-left: 0px; font-weight: 700; }
.md-toc-h2 .md-toc-inner { margin-left: 2em; }
.md-toc-h3 .md-toc-inner { margin-left: 4em; }
.md-toc-h4 .md-toc-inner { margin-left: 6em; }
.md-toc-h5 .md-toc-inner { margin-left: 8em; }
.md-toc-h6 .md-toc-inner { margin-left: 10em; }
@media screen and (max-width: 48em) {
  .md-toc-h3 .md-toc-inner { margin-left: 3.5em; }
  .md-toc-h4 .md-toc-inner { margin-left: 5em; }
  .md-toc-h5 .md-toc-inner { margin-left: 6.5em; }
  .md-toc-h6 .md-toc-inner { margin-left: 8em; }
}
a.md-toc-inner { font-size: inherit; font-style: inherit; font-weight: inherit; line-height: inherit; }
.footnote-line a:not(.reversefootnote) { color: inherit; }
.md-attr { display: none; }
.md-fn-count::after { content: "."; }
code, pre, samp, tt { font-family: var(--monospace); }
kbd { margin: 0px 0.1em; padding: 0.1em 0.6em; font-size: 0.8em; color: rgb(36, 39, 41); background: rgb(255, 255, 255); border: 1px solid rgb(173, 179, 185); border-radius: 3px; box-shadow: rgba(12, 13, 14, 0.2) 0px 1px 0px, rgb(255, 255, 255) 0px 0px 0px 2px inset; white-space: nowrap; vertical-align: middle; }
.md-comment { color: rgb(162, 127, 3); opacity: 0.8; font-family: var(--monospace); }
code { text-align: left; vertical-align: initial; }
a.md-print-anchor { white-space: pre !important; border-width: initial !important; border-style: none !important; border-color: initial !important; display: inline-block !important; position: absolute !important; width: 1px !important; right: 0px !important; outline: 0px !important; background: 0px 0px !important; text-decoration: initial !important; text-shadow: initial !important; }
.md-inline-math .MathJax_SVG .noError { display: none !important; }
.html-for-mac .inline-math-svg .MathJax_SVG { vertical-align: 0.2px; }
.md-math-block .MathJax_SVG_Display { text-align: center; margin: 0px; position: relative; text-indent: 0px; max-width: none; max-height: none; min-height: 0px; min-width: 100%; width: auto; overflow-y: hidden; display: block !important; }
.MathJax_SVG_Display, .md-inline-math .MathJax_SVG_Display { width: auto; margin: inherit; display: inline-block !important; }
.MathJax_SVG .MJX-monospace { font-family: var(--monospace); }
.MathJax_SVG .MJX-sans-serif { font-family: sans-serif; }
.MathJax_SVG { display: inline; font-style: normal; font-weight: 400; line-height: normal; zoom: 90%; text-indent: 0px; text-align: left; text-transform: none; letter-spacing: normal; word-spacing: normal; overflow-wrap: normal; white-space: nowrap; float: none; direction: ltr; max-width: none; max-height: none; min-width: 0px; min-height: 0px; border: 0px; padding: 0px; margin: 0px; }
.MathJax_SVG * { transition: none 0s ease 0s; }
.MathJax_SVG_Display svg { vertical-align: middle !important; margin-bottom: 0px !important; margin-top: 0px !important; }
.os-windows.monocolor-emoji .md-emoji { font-family: "Segoe UI Symbol", sans-serif; }
.md-diagram-panel > svg { max-width: 100%; }
[lang="flow"] svg, [lang="mermaid"] svg { max-width: 100%; height: auto; }
[lang="mermaid"] .node text { font-size: 1rem; }
table tr th { border-bottom: 0px; }
video { max-width: 100%; display: block; margin: 0px auto; }
iframe { max-width: 100%; width: 100%; border: none; }
.highlight td, .highlight tr { border: 0px; }
svg[id^="mermaidChart"] { line-height: 1em; }
mark { background: rgb(255, 255, 0); color: rgb(0, 0, 0); }
.md-html-inline .md-plain, .md-html-inline strong, mark .md-inline-math, mark strong { color: inherit; }
mark .md-meta { color: rgb(0, 0, 0); opacity: 0.3 !important; }
@media print {
  .typora-export h1, .typora-export h2, .typora-export h3, .typora-export h4, .typora-export h5, .typora-export h6 { break-inside: avoid; }
}


.CodeMirror { height: auto; }
.CodeMirror.cm-s-inner { background: inherit; }
.CodeMirror-scroll { overflow: auto hidden; z-index: 3; }
.CodeMirror-gutter-filler, .CodeMirror-scrollbar-filler { background-color: rgb(255, 255, 255); }
.CodeMirror-gutters { border-right: 1px solid rgb(221, 221, 221); background: inherit; white-space: nowrap; }
.CodeMirror-linenumber { padding: 0px 3px 0px 5px; text-align: right; color: rgb(153, 153, 153); }
.cm-s-inner .cm-keyword { color: rgb(119, 0, 136); }
.cm-s-inner .cm-atom, .cm-s-inner.cm-atom { color: rgb(34, 17, 153); }
.cm-s-inner .cm-number { color: rgb(17, 102, 68); }
.cm-s-inner .cm-def { color: rgb(0, 0, 255); }
.cm-s-inner .cm-variable { color: rgb(0, 0, 0); }
.cm-s-inner .cm-variable-2 { color: rgb(0, 85, 170); }
.cm-s-inner .cm-variable-3 { color: rgb(0, 136, 85); }
.cm-s-inner .cm-string { color: rgb(170, 17, 17); }
.cm-s-inner .cm-property { color: rgb(0, 0, 0); }
.cm-s-inner .cm-operator { color: rgb(152, 26, 26); }
.cm-s-inner .cm-comment, .cm-s-inner.cm-comment { color: rgb(170, 85, 0); }
.cm-s-inner .cm-string-2 { color: rgb(255, 85, 0); }
.cm-s-inner .cm-meta { color: rgb(85, 85, 85); }
.cm-s-inner .cm-qualifier { color: rgb(85, 85, 85); }
.cm-s-inner .cm-builtin { color: rgb(51, 0, 170); }
.cm-s-inner .cm-bracket { color: rgb(153, 153, 119); }
.cm-s-inner .cm-tag { color: rgb(17, 119, 0); }
.cm-s-inner .cm-attribute { color: rgb(0, 0, 204); }
.cm-s-inner .cm-header, .cm-s-inner.cm-header { color: rgb(0, 0, 255); }
.cm-s-inner .cm-quote, .cm-s-inner.cm-quote { color: rgb(0, 153, 0); }
.cm-s-inner .cm-hr, .cm-s-inner.cm-hr { color: rgb(153, 153, 153); }
.cm-s-inner .cm-link, .cm-s-inner.cm-link { color: rgb(0, 0, 204); }
.cm-negative { color: rgb(221, 68, 68); }
.cm-positive { color: rgb(34, 153, 34); }
.cm-header, .cm-strong { font-weight: 700; }
.cm-del { text-decoration: line-through; }
.cm-em { font-style: italic; }
.cm-link { text-decoration: underline; }
.cm-error { color: red; }
.cm-invalidchar { color: red; }
.cm-constant { color: rgb(38, 139, 210); }
.cm-defined { color: rgb(181, 137, 0); }
div.CodeMirror span.CodeMirror-matchingbracket { color: rgb(0, 255, 0); }
div.CodeMirror span.CodeMirror-nonmatchingbracket { color: rgb(255, 34, 34); }
.cm-s-inner .CodeMirror-activeline-background { background: inherit; }
.CodeMirror { position: relative; overflow: hidden; }
.CodeMirror-scroll { height: 100%; outline: 0px; position: relative; box-sizing: content-box; background: inherit; }
.CodeMirror-sizer { position: relative; }
.CodeMirror-gutter-filler, .CodeMirror-hscrollbar, .CodeMirror-scrollbar-filler, .CodeMirror-vscrollbar { position: absolute; z-index: 6; display: none; }
.CodeMirror-vscrollbar { right: 0px; top: 0px; overflow: hidden; }
.CodeMirror-hscrollbar { bottom: 0px; left: 0px; overflow: hidden; }
.CodeMirror-scrollbar-filler { right: 0px; bottom: 0px; }
.CodeMirror-gutter-filler { left: 0px; bottom: 0px; }
.CodeMirror-gutters { position: absolute; left: 0px; top: 0px; padding-bottom: 30px; z-index: 3; }
.CodeMirror-gutter { white-space: normal; height: 100%; box-sizing: content-box; padding-bottom: 30px; margin-bottom: -32px; display: inline-block; }
.CodeMirror-gutter-wrapper { position: absolute; z-index: 4; background: 0px 0px !important; border: none !important; }
.CodeMirror-gutter-background { position: absolute; top: 0px; bottom: 0px; z-index: 4; }
.CodeMirror-gutter-elt { position: absolute; cursor: default; z-index: 4; }
.CodeMirror-lines { cursor: text; }
.CodeMirror pre { border-radius: 0px; border-width: 0px; background: 0px 0px; font-family: inherit; font-size: inherit; margin: 0px; white-space: pre; overflow-wrap: normal; color: inherit; z-index: 2; position: relative; overflow: visible; }
.CodeMirror-wrap pre { overflow-wrap: break-word; white-space: pre-wrap; word-break: normal; }
.CodeMirror-code pre { border-right: 30px solid transparent; width: fit-content; }
.CodeMirror-wrap .CodeMirror-code pre { border-right: none; width: auto; }
.CodeMirror-linebackground { position: absolute; left: 0px; right: 0px; top: 0px; bottom: 0px; z-index: 0; }
.CodeMirror-linewidget { position: relative; z-index: 2; overflow: auto; }
.CodeMirror-wrap .CodeMirror-scroll { overflow-x: hidden; }
.CodeMirror-measure { position: absolute; width: 100%; height: 0px; overflow: hidden; visibility: hidden; }
.CodeMirror-measure pre { position: static; }
.CodeMirror div.CodeMirror-cursor { position: absolute; visibility: hidden; border-right: none; width: 0px; }
.CodeMirror div.CodeMirror-cursor { visibility: hidden; }
.CodeMirror-focused div.CodeMirror-cursor { visibility: inherit; }
.cm-searching { background: rgba(255, 255, 0, 0.4); }
@media print {
  .CodeMirror div.CodeMirror-cursor { visibility: hidden; }
}


/* 全局属性 */
#write {
  max-width: 860px;
  font-size: 16px;
  color: black;
  padding: 0 10px;
  line-height: 1.6;
  word-spacing: 0px;
  letter-spacing: 0px;
  word-break: break-word;
  word-wrap: break-word;
  text-align: left;
  font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, 'PingFang SC', Cambria, Cochin, Georgia, Times, 'Times New Roman', serif;
}

/*段落*/
#write p {
  font-size: 16px;
  padding-top: 8px;
  padding-bottom: 8px;
  margin: 0;
  line-height: 26px;
  color: black;
}


/*标题*/
#write h1,
#write h2,
#write h3,
#write h4,
#write h5,
#write h6 {
  margin-top: 30px;
  margin-bottom: 15px;
  padding: 0px;
  font-weight: bold;
  color: black;
}
#write h1 {
  font-size: 1.5rem;
}
#write h2 {
  font-size: 1.3rem;
  border-bottom: 2px solid rgb(239, 112, 96);
}
#write h2 span {
 display: inline-block;
 font-weight: bold;
 background: rgb(239, 112, 96);
 color: #ffffff;
 padding: 3px 10px 1px;
 border-top-right-radius: 3px;
 border-top-left-radius: 3px;
 margin-right: 3px;
}
#write h2:after {
 display: inline-block;
 content: "";
 vertical-align: bottom;
 border-bottom: 36px solid #efebe9;
 border-right: 20px solid transparent;
}
#write h3 {
  font-size: 1.2rem;
}
#write h4 {
  font-size: 1.1rem;
}
#write h5 {
  font-size: 1rem;
}
#write h6 {
  font-size: 1rem;
}

/*列表*/
#write ul,
#write ol {
  margin-top: 8px;
  margin-bottom: 8px;
  padding-left: 25px;
  color: black;
}
#write ul {
  list-style-type: disc;
}
#write ul ul {
  list-style-type: square;
}
#write ol {
  list-style-type: decimal;
}
#write li section {
  margin-top: 5px;
  margin-bottom: 5px;
  line-height: 26px;
  text-align: left;
  color: rgb(1,1,1); /* 只要是纯黑色微信编辑器就会把color这个属性吞掉。。。*/
  font-weight: 500;
}

/*引用*/
#write blockquote {
  display: block;
  font-size: 0.9em;
  overflow: auto;
  overflow-scrolling: touch;
  border-left: 3px solid rgb(239, 112, 96);
  color: #6a737d;
  padding: 10px 10px 10px 20px;
  margin-bottom: 20px;
  margin-top: 20px;
  background: #fff9f9;
}
#write blockquote p {
  margin: 0px;
  color: black;
  line-height: 26px;
}

/*链接*/
#write a {
  text-decoration: none;
  word-wrap: break-word;
  font-weight: bold;
  border-bottom: 1px solid #1e6bb8;
  color: rgb(239, 112, 96);
  border-bottom: 1px solid rgb(239, 112, 96);
}

/*行内代码*/
#write p code,
#write li code {
  font-size: 14px;
  word-wrap: break-word;
  padding: 2px 4px;
  border-radius: 4px;
  margin: 0 2px;
  color:  rgb(239, 112, 96);;
  background-color: rgba(27,31,35,.05);
  font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;
  word-break: break-all;
}

/*图片*/
#write img {
  display: block;
  margin: 0 auto;
  max-width: 100%;
}

/*表格*/
#write table {
  display: table;
  text-align: left;
}
#write tbody {
  border: 0;
}
#write table tr {
  border: 0;
  border-top: 1px solid #ccc;
  background-color: white;
}
#write table tr:nth-child(2n) {
  background-color: #F8F8F8;
}
#write table tr th,
#write table tr td {
  font-size: 16px;
  border: 1px solid #ccc;
  padding: 5px 10px;
  text-align: left;
}
#write table tr th {
  font-weight: bold;
  background-color: #f0f0f0;
}

/* 行内代码 */
#write span code, #write li code {
 color: rgb(239, 112, 96);
}

/* 脚注上标 */
#write .md-footnote {
 font-weight: bold;
 color: rgb(239, 112, 96);
}
#write .md-footnote > .md-text:before {
 content: '['
}
#write .md-footnote > .md-text:after {
 content: ']'
}

/* 脚注 */
#write .md-def-name {
  padding-right: 1.8ch;
}
#write .md-def-name:before {
  content: '[';
  color: #000;
}
#write .md-def-name:after {
  color: #000;
}

/* 代码块主题 */
.md-fences:before {
  content: ' ';
  display: block;
  width: 100%;
  background-size: 40px;
  background-repeat: no-repeat;
  background-color: #282c34;
  margin-bottom: -7px;
  border-radius: 5px;
  background-position: 10px 10px;
}
.cm-s-inner.CodeMirror {
  padding-top: .5rem;
  padding-bottom: .5rem;
  background-color: #292d3e;
  color: #a6accd;
  font-family: Consolas;
  border-radius: 4px;
}
.CodeMirror-lines {
  padding-left: 4px;
}
.cm-s-inner .cm-keyword {
  color: #c792ea;
}
.cm-s-inner .cm-operator {
  color: #89ddff;
}
.cm-s-inner .cm-variable-2 {
  color: #eeffff;
}
.cm-s-inner .cm-variable-3,
.cm-s-inner .cm-type {
  color: #f07178;
}
.cm-s-inner .cm-builtin {
  color: #ffcb6b;
}
.cm-s-inner .cm-atom {
  color: #f78c6c;
}
.cm-s-inner .cm-number {
  color: #ff5370;
}
.cm-s-inner .cm-def {
  color: #82aaff;
}
.cm-s-inner .cm-string {
  color: #c3e88d;
}
.cm-s-inner .cm-string-2 {
  color: #f07178;
}
.cm-s-inner .cm-comment {
  color: #676e95;
}
.cm-s-inner .cm-variable {
  color: #f07178;
}
.cm-s-inner .cm-tag {
  color: #ff5370;
}
.cm-s-inner .cm-meta {
  color: #ffcb6b;
}
.cm-s-inner .cm-attribute {
  color: #c792ea;
}
.cm-s-inner .cm-property {
  color: #c792ea;
}
.cm-s-inner .cm-qualifier {
  color: #decb6b;
}
.cm-s-inner .cm-variable-3,
.cm-s-inner .cm-type {
  color: #decb6b;
}
.cm-s-inner .cm-error {
  color: rgba(255, 255, 255, 1);
  background-color: #ff5370;
}
.cm-s-inner .CodeMirror-matchingbracket {
  text-decoration: underline;
  color: white !important;
}
.CodeMirror div.CodeMirror-cursor {
  border-left: 1px solid rgb(239, 112, 96);
  z-index: 3;
}

    #write{margin:50px 50px 50px 260px; padding: 20px 60px; max-width: unset; background: #fff;border: 1px solid #E6ECF1; box-shadow: 0 2px 8px rgba(116, 129, 141, .08); border-radius: 4px}
    .BlogAnchor {width: 210px !important}
 
    @media screen and (max-width: 1620px) {
        #write{margin:0px 40px 40px 250px; padding: 10px 160px;}
        .BlogAnchor {width: 210px !important}
    }

    @media screen and (max-width: 1320px) {
        #write{margin:0px 20px 20px 220px; padding: 10px 40px;}
    }
 
 
    @media screen and (max-width: 820px) {
        #write{margin:0px; padding: 10px; font-size: 14px; line-height: 160%}
        .BlogAnchor {display: none;}
        h2{ font-size: 28px; }
        h3{ font-size: 22px; }
}

/*scroll_style*/
.AnchorContent {
    overflow-y: auto !important;
    overflow-x: hidden !important;
    margin-right:0 !important
}
 
.AnchorContent::-webkit-scrollbar {
    width: 6px;
    height: 10px;
    background-color: rgba(0,0,0,0) !important;
    position: absolute;
    right: 10px
}
.AnchorContent::-webkit-scrollbar-track {
    -webkit-box-shadow: none;
    border-radius: 0;
    background-color: rgba(0,0,0,0) !important
}
.AnchorContent::-webkit-scrollbar-thumb {
    border-radius: 0px;
    -webkit-box-shadow: none;
    background-color: none;
    opacity: 0 !important;
    transition: all .3s
}
 
.AnchorContent:hover::-webkit-scrollbar-thumb {
    border-radius: 0px;
    -webkit-box-shadow: none;
    background-color: #d4d6d8;
    border-right:2px solid #fff;
    opacity:1;
}
 
.AnchorContent::-webkit-scrollbar-thumb:hover {
    background-color: #c0c2c4 !important
}
 
 
/*导航*/
    .BlogAnchor {
        background: #fff;
        padding: 0px 0 ;
        line-height: 180%;
        position: fixed;
        height: 100%;
        width: 20%;
        left: 30px;
        top: 1%;
        border-right: 1px solid #E6ECF1;
 
    }
    .BlogAnchor p {
        font-size: 20px;
        color: #444;
        margin: 0 16px;
        text-align: center;
    }
    .BlogAnchor .AnchorContent {
        padding: 0px 0px;
        overflow: auto;
    }
    .BlogAnchor li{
        text-indent: 0rem;
        font-size: 14px;
        list-style: none;
        padding: 0 0px;
    }
    .BlogAnchor li .nav_item{
        padding:0px 16px;
 
    }
 
 
    .BlogAnchor li .item_h1{
        margin-left: 0rem;
        padding-left:0px;
        font-weight: bold;
    }
    .BlogAnchor li .item_h2{
        margin-left: 0;
        font-size: 0.95rem;
        padding-left: 0px;
        color: #708090
    }
    .BlogAnchor li .nav_item.current{
        color: #087cd2;
        background-color: rgba(238,242,249,.8);
        border-left:0px solid #087cd2;
        text-indent: 2px;
        font-weight: bold;
 
    }
    #AnchorContentToggle {
        font-size: 13px;
        font-weight: normal;
        color: #FFF;
        display: inline-block;
        line-height: 20px;
        background: rgb(7,196,189);
        font-style: normal;
        padding: 1px 8px;
    }
    .BlogAnchor a:hover {
        color: #087cd2  ;
        background:rgba(238,242,249,.5);
    }
    .BlogAnchor a {
        text-decoration: none;
        color: #333;
        display: block;
        border-radius: 0px;
    }

</style>
</head>
<body class='typora-export os-windows'>
<div id='write'  class=''><h1><a name="nodejs" class="md-header-anchor"></a><span>Node.js</span></h1><h2><a name="nodejs-n358" class="md-header-anchor"></a><span>Node.js</span></h2><p><img src="Node.js.assets/image-20220305203624269.png" referrerpolicy="no-referrer" alt="image-20220305203624269"></p><p>&nbsp;</p><p><img src="Node.js.assets/image-20220305202841736.png" referrerpolicy="no-referrer" alt="image-20220305202841736"></p><h2><a name="js解析引擎" class="md-header-anchor"></a><span>js解析引擎</span></h2><p><img src="Node.js.assets/image-20220305202958939.png" referrerpolicy="no-referrer" alt="image-20220305202958939"></p><p><img src="Node.js.assets/image-20220305203127624.png" referrerpolicy="no-referrer" alt="image-20220305203127624"></p><h2><a name="运行环境" class="md-header-anchor"></a><span>运行环境</span></h2><p><img src="Node.js.assets/image-20220305203320571.png" referrerpolicy="no-referrer" alt="image-20220305203320571"></p><p>&nbsp;</p><p><img src="Node.js.assets/image-20220305204058258.png" referrerpolicy="no-referrer" alt="image-20220305204058258"></p><h1><a name="fs文件系统模块" class="md-header-anchor"></a><span>fs文件系统模块</span></h1><p>&nbsp;</p><p><span>fs模块相当于是标准库，node就是类似于Java中的jre环境，提供了很多标准库使用。</span></p><p><img src="Node.js.assets/image-20220305205010817.png" referrerpolicy="no-referrer" alt="image-20220305205010817"></p><h2><a name="fsreadfile使用" class="md-header-anchor"></a><span>fs.readFile()使用</span></h2><ul><li><span>第一个参数是文件路径，可以是相对的，也可以是绝对路径</span></li><li><span>第二个参数是文件读取的编码格式</span></li><li><span>第三个参数是回调函数</span></li></ul><p><span>readFile返回的是一个Promist对象，有调用失败和调用成功的两个回调函数。Node的设计是错误先行，所以一般的回调函数的第一个回调参数都是error，第二个参数才是result</span></p><p><img src="Node.js.assets/image-20220305205213838.png" referrerpolicy="no-referrer" alt="image-20220305205213838"></p><p><span>readFile返回一个promise对象</span></p><p><span>无论是读取文件还是写文件返回的都是一个Promise对象，会有一个回调函数，node的设计是错误先行，所以第一个参数是err，第二个参数是res</span></p><h2><a name="fswritefile使用" class="md-header-anchor"></a><span>fs.writeFile()使用</span></h2><ul><li><span>第一个参数是写出的文件路径</span></li><li><span>第二个参数是文件的内容</span></li><li><span>第三个参数是可选的，表示以什么样的编码格式写出，默认是utf8</span></li><li><span>第四个参数是回调函数，同样的回调函数的第一个参数是err，第二个参数是result，注意node的设计是回调先行的，所以回到函数的第一个参数基本上都是err，第二个参数才是result</span></li></ul><p><img src="Node.js.assets/image-20220312234213462.png" referrerpolicy="no-referrer" alt="image-20220312234213462"></p><p><span>write函数会自动将原有的文件覆盖，直接覆盖掉原有的文件。</span></p><h3><a name="案例" class="md-header-anchor"></a><span>案例</span></h3><p><img src="Node.js.assets/image-20220312234757615.png" referrerpolicy="no-referrer" alt="image-20220312234757615"></p><h3><a name="步骤" class="md-header-anchor"></a><span>步骤</span></h3><p><img src="Node.js.assets/image-20220312234842513.png" referrerpolicy="no-referrer" alt="image-20220312234842513"></p><p><span>forEach()函数，和join函数拼接，方便数据的处理</span></p><h2><a name="路径拼接问题" class="md-header-anchor"></a><span>路径拼接问题</span></h2><p><span>这个问题不是说不能执行js文件，执行js文件可以使用的命令是：node + 文件的存放位置，只是说js文件在读取其他文件的时候会以node命令执行的目录拼接上定义的相对路径，这是路径拼接的问题。</span></p><p><img src="Node.js.assets/image-20220313000209623.png" referrerpolicy="no-referrer" alt="image-20220313000209623"></p><p><span>在终端中，只要node的路径中，是相对路径，那么node就会以 当前的目录 + 代码中所给的路径作为最终的路径，所以即使写的node + 正确的路径，但是拼接的也是错误的路径。</span></p><p><span>#＃# 代码演示</span></p><pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="javascript" style="break-inside: unset;"><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang="javascript"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 7px; left: 41px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 33px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre><div class="CodeMirror-linenumber CodeMirror-gutter-elt"><div>20</div></div></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: -33px; width: 33px;"></div><div class="CodeMirror-gutter-wrapper CodeMirror-activeline-gutter" style="left: -33px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 24px;">1</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-comment">/**</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -33px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 24px;">2</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-comment"> * 导入fs模块，运行</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -33px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 24px;">3</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-comment"> */</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -33px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 24px;">4</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -33px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 24px;">5</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-keyword">const</span> <span class="cm-def">fs</span> <span class="cm-operator">=</span> <span class="cm-variable">require</span>(<span class="cm-string">'fs'</span>);</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -33px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 24px;">6</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -33px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 24px;">7</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-comment">/**</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -33px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 24px;">8</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-comment"> * 给定一个回调函数，表示成功运行或者失败运行时的回调</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -33px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 24px;">9</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-comment"> * node的设计是错误先行，所以第一个参数是错误的回调信息</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -33px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 24px;">10</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-comment"> */</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -33px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 24px;">11</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">fs</span>.<span class="cm-property">readFile</span>(<span class="cm-string">'./1.txt'</span>,<span class="cm-string">'utf8'</span>, <span class="cm-keyword">function</span>(<span class="cm-def">err</span>, <span class="cm-def">res</span>) {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -33px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 24px;">12</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">console</span>.<span class="cm-property">log</span>(<span class="cm-variable-2">err</span>)</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -33px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 24px;">13</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">console</span>.<span class="cm-property">log</span>(<span class="cm-variable-2">res</span>)</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -33px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 24px;">14</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">})</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -33px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 24px;">15</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -33px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 24px;">16</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">fs</span>.<span class="cm-property">readFile</span>(<span class="cm-string">'./2'</span>,<span class="cm-string">'utf8'</span>,(<span class="cm-def">err</span>,<span class="cm-def">res</span>) <span class="cm-operator">=&gt;</span> {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -33px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 24px;">17</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">console</span>.<span class="cm-property">log</span>(<span class="cm-variable-2">err</span>)</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -33px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 24px;">18</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">console</span>.<span class="cm-property">log</span>(<span class="cm-variable-2">res</span>)</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -33px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 24px;">19</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">})</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -33px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 24px;">20</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom: 0px solid transparent; top: 448px;"></div><div class="CodeMirror-gutters" style="height: 448px;"><div class="CodeMirror-gutter CodeMirror-linenumbers" style="width: 32px;"></div></div></div></div></pre><p><span>控制台信息</span></p><pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="shell" style="break-inside: unset;"><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang="shell"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 7px; left: 41px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 33px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre><div class="CodeMirror-linenumber CodeMirror-gutter-elt"><div>16</div></div></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: -33px; width: 33px;"></div><div class="CodeMirror-gutter-wrapper CodeMirror-activeline-gutter" style="left: -33px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 24px;">1</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">D:\OneDrive <span class="cm-attribute">-</span> luckywords\CodeWork\学习笔记\Node.js.assets\fs文件系统&gt;node fs-readFile函数使用.js</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -33px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 24px;">2</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">Hello World</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -33px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 24px;">3</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">null</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -33px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 24px;">4</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -33px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 24px;">5</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">D:\OneDrive <span class="cm-attribute">-</span> luckywords\CodeWork\学习笔记\Node.js.assets\fs文件系统&gt;node fs-readFile函数使用.js</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -33px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 24px;">6</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">[Error: ENOENT: no such file or directory, open <span class="cm-string">'D:\OneDrive - luckywords\CodeWork\学习笔记\Node.js.assets\fs文件系统\2'</span>] {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -33px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 24px;">7</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">  errno: <span class="cm-attribute">-4058</span>,</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -33px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 24px;">8</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">  code: <span class="cm-string">'ENOENT'</span>,</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -33px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 24px;">9</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">  syscall: <span class="cm-string">'open'</span>,</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -33px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 24px;">10</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">  path: <span class="cm-string">'D:\\OneDrive - luckywords\\CodeWork\\学习笔记\\Node.js.assets\\fs文件系统\\2'</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -33px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 24px;">11</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">}</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -33px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 24px;">12</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">undefined</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -33px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 24px;">13</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">null</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -33px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 24px;">14</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">Hello World</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -33px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 24px;">15</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -33px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 24px;">16</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">D:\OneDrive <span class="cm-attribute">-</span> luckywords\CodeWork\学习笔记\Node.js.assets\fs文件系统&gt;</span></pre></div></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom: 0px solid transparent; top: 448px;"></div><div class="CodeMirror-gutters" style="height: 448px;"><div class="CodeMirror-gutter CodeMirror-linenumbers" style="width: 32px;"></div></div></div></div></pre><h3><a name="路径错误问题" class="md-header-anchor"></a><span>路径错误问题</span></h3><p><span>在控制台不同的路径执行相同的文件时，如果在原有的读取路径中写成的是相对位置，那么node会以控制台的路径拼接上相对路径，而不是文件的路径拼接上相对路径。</span></p><pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="javascript"><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang="javascript"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 7px; left: 34px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 26px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre><div class="CodeMirror-linenumber CodeMirror-gutter-elt"><div>7</div></div></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: -26px; width: 26px;"></div><div class="CodeMirror-gutter-wrapper CodeMirror-activeline-gutter" style="left: -26px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 17px;">1</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-comment">/**</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -26px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 17px;">2</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-comment"> * 演示不同路径下执行读取的错误，路径拼接错误</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -26px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 17px;">3</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-comment"> */</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -26px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 17px;">4</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">fs</span>.<span class="cm-property">readFile</span>(<span class="cm-string">'./1.txt'</span>,<span class="cm-string">'utf8'</span>,(<span class="cm-def">err</span>,<span class="cm-def">res</span>) <span class="cm-operator">=&gt;</span> {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -26px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 17px;">5</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">console</span>.<span class="cm-property">log</span>(<span class="cm-variable-2">err</span>)</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -26px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 17px;">6</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">console</span>.<span class="cm-property">log</span>(<span class="cm-variable-2">res</span>)</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -26px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 17px;">7</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">})</span></pre></div></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom: 0px solid transparent; top: 157px;"></div><div class="CodeMirror-gutters" style="height: 157px;"><div class="CodeMirror-gutter CodeMirror-linenumbers" style="width: 25px;"></div></div></div></div></pre><p>&nbsp;</p><pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="shell" style="break-inside: unset;"><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang="shell"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 7px; left: 41px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 33px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre><div class="CodeMirror-linenumber CodeMirror-gutter-elt"><div>16</div></div></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: -33px; width: 33px;"></div><div class="CodeMirror-gutter-wrapper CodeMirror-activeline-gutter" style="left: -33px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 24px;">1</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">D:\OneDrive <span class="cm-attribute">-</span> luckywords\CodeWork\学习笔记\Node.js.assets\fs文件系统&gt;node fs-readFile函数使用.js</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -33px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 24px;">2</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">null</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -33px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 24px;">3</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">Hello World</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -33px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 24px;">4</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -33px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 24px;">5</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">D:\OneDrive <span class="cm-attribute">-</span> luckywords\CodeWork\学习笔记\Node.js.assets\fs文件系统&gt;cd ../..</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -33px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 24px;">6</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -33px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 24px;">7</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">D:\OneDrive <span class="cm-attribute">-</span> luckywords\CodeWork\学习笔记&gt;node Node.js.assets/fs文件系统/fs-readFile函数使用.js</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -33px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 24px;">8</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">[Error: ENOENT: no such file or directory, open <span class="cm-string">'D:\OneDrive - luckywords\CodeWork\学习笔记\1.txt'</span>] {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -33px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 24px;">9</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">  errno: <span class="cm-attribute">-4058</span>,</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -33px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 24px;">10</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">  code: <span class="cm-string">'ENOENT'</span>,</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -33px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 24px;">11</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">  syscall: <span class="cm-string">'open'</span>,</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -33px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 24px;">12</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">  path: <span class="cm-string">'D:\\OneDrive - luckywords\\CodeWork\\学习笔记\\1.txt'</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -33px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 24px;">13</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">}</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -33px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 24px;">14</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">undefined</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -33px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 24px;">15</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -33px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 24px;">16</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">D:\OneDrive <span class="cm-attribute">-</span> luckywords\CodeWork\学习笔记&gt;</span></pre></div></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom: 0px solid transparent; top: 426px;"></div><div class="CodeMirror-gutters" style="height: 426px;"><div class="CodeMirror-gutter CodeMirror-linenumbers" style="width: 32px;"></div></div></div></div></pre><h2><a name="解决方案" class="md-header-anchor"></a><span>解决方案</span></h2><p><img src="Node.js.assets/image-20220314153826027.png" referrerpolicy="no-referrer" alt="image-20220314153826027"></p><p><span>使用node给定的属性，__.dirname表示当前文件所处的路径。</span></p><h1><a name="path路径模块" class="md-header-anchor"></a><span>path路径模块</span></h1><p><span>涉及到路径的拼接问题都需要用到path模块，不要直接使用+号进行操作，涉及到路径的问题一定使用path模块进行处理。加号不能处理一些特殊的错误。</span></p><ul><li><span>例如不小心在拼接字符串时，多加上了一个点，path模块可以直接帮我们去掉，但是+号会原样的拼接字符串。</span></li></ul><p><img src="Node.js.assets/image-20220314154146790.png" referrerpolicy="no-referrer" alt="image-20220314154146790"></p><h3><a name="拼接任意多个字符" class="md-header-anchor"></a><span>拼接任意多个字符</span></h3><p><img src="Node.js.assets/image-20220314154249674.png" referrerpolicy="no-referrer" alt="image-20220314154249674"></p><h2><a name="获取文件名" class="md-header-anchor"></a><span>获取文件名</span></h2><p><span>使用 </span><code>path.basename(path)</code><span> 或者是 第二个参数是拓展名</span></p><p><img src="Node.js.assets/image-20220314155002935.png" referrerpolicy="no-referrer" alt="image-20220314155002935"></p><p><span>讲传递的路径的最后一部分返回，即返回文件名或者是文件夹名，如果是文件名，那么拓展名也可以获取。如果不想获取文件的拓展名，那么传入对应的拓展名，必须要知道对应的拓展名。</span></p><h2><a name="获取文件的拓展名" class="md-header-anchor"></a><span>获取文件的拓展名</span></h2><p><span>获取文件的拓展名</span><code>path.extname()</code></p><pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="javascript"><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang="javascript"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 7px; left: 41px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 33px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre><div class="CodeMirror-linenumber CodeMirror-gutter-elt"><div>10</div></div></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: -33px; width: 33px;"></div><div class="CodeMirror-gutter-wrapper CodeMirror-activeline-gutter" style="left: -33px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 24px;">1</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-keyword">let</span> <span class="cm-def">val</span> <span class="cm-operator">=</span> <span class="cm-variable">path</span>.<span class="cm-property">join</span>(<span class="cm-variable">__dirname</span>,<span class="cm-string">'1.txt'</span>)</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -33px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 24px;">2</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -33px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 24px;">3</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">fs</span>.<span class="cm-property">readFile</span>(<span class="cm-variable">path</span>.<span class="cm-property">join</span>(<span class="cm-variable">__dirname</span>,<span class="cm-string">'1.txt'</span>),<span class="cm-string">'utf8'</span>,(<span class="cm-def">err</span>,<span class="cm-def">res</span>) <span class="cm-operator">=&gt;</span> {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -33px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 24px;">4</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">console</span>.<span class="cm-property">log</span>(<span class="cm-variable-2">err</span>);</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -33px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 24px;">5</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp; &nbsp;<span class="cm-variable">console</span>.<span class="cm-property">log</span>(<span class="cm-variable-2">res</span>);</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -33px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 24px;">6</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">})</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -33px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 24px;">7</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -33px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 24px;">8</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">console</span>.<span class="cm-property">log</span>(<span class="cm-variable">path</span>.<span class="cm-property">basename</span>(<span class="cm-variable">val</span>));</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -33px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 24px;">9</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">console</span>.<span class="cm-property">log</span>(<span class="cm-variable">path</span>.<span class="cm-property">basename</span>(<span class="cm-variable">val</span>,<span class="cm-string">'txt'</span>))</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -33px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 24px;">10</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">console</span>.<span class="cm-property">log</span>(<span class="cm-variable">path</span>.<span class="cm-property">basename</span>(<span class="cm-variable">val</span>,<span class="cm-variable">path</span>.<span class="cm-property">extname</span>(<span class="cm-variable">val</span>)))</span></pre></div></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom: 0px solid transparent; top: 224px;"></div><div class="CodeMirror-gutters" style="height: 224px;"><div class="CodeMirror-gutter CodeMirror-linenumbers" style="width: 32px;"></div></div></div></div></pre><p><span>控制台输出</span></p><pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="shell"><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang="shell"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 7px; left: 34px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 26px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre><div class="CodeMirror-linenumber CodeMirror-gutter-elt"><div>8</div></div></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: -26px; width: 26px;"></div><div class="CodeMirror-gutter-wrapper CodeMirror-activeline-gutter" style="left: -26px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 17px;">1</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">D:\OneDrive <span class="cm-attribute">-</span> luckywords\CodeWork\学习笔记&gt;node Node.js.assets/fs文件系统/fs-readFile函数使用.js</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -26px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 17px;">2</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-number">1</span>.txt</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -26px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 17px;">3</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-number">1</span>.</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -26px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 17px;">4</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-number">1</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -26px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 17px;">5</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">null</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -26px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 17px;">6</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">Hello World</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -26px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 17px;">7</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -26px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 17px;">8</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">D:\OneDrive <span class="cm-attribute">-</span> luckywords\CodeWork\学习笔记&gt;</span></pre></div></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom: 0px solid transparent; top: 202px;"></div><div class="CodeMirror-gutters" style="height: 202px;"><div class="CodeMirror-gutter CodeMirror-linenumbers" style="width: 25px;"></div></div></div></div></pre><h2><a name="案例-n435" class="md-header-anchor"></a><span>案例</span></h2><p><span>将一个完成的html文拆分成三个文件</span></p><p><img src="Node.js.assets/image-20220314160455195.png" referrerpolicy="no-referrer" alt="image-20220314160455195"></p><p><img src="Node.js.assets/image-20220314163340953.png" referrerpolicy="no-referrer" alt="image-20220314163340953"></p><h1><a name="http模块" class="md-header-anchor"></a><span>http模块</span></h1><p><img src="Node.js.assets/image-20220314163455676.png" referrerpolicy="no-referrer" alt="image-20220314163455676"></p><h2><a name="ip地址与服务器" class="md-header-anchor"></a><span>IP地址与服务器</span></h2><p><img src="Node.js.assets/image-20220314163932463.png" referrerpolicy="no-referrer" alt="image-20220314163932463"></p><p><span>整个IP地址是很重要的内容，有时间还是有好好的补补TCP/IP的知识。</span></p><p><img src="Node.js.assets/image-20220314164216745.png" referrerpolicy="no-referrer" alt="image-20220314164216745"></p><p><img src="Node.js.assets/image-20220314164541160.png" referrerpolicy="no-referrer" alt="image-20220314164541160"></p><h2><a name="使用步骤" class="md-header-anchor"></a><span>使用步骤</span></h2><ul><li><span>导入http模块</span></li><li><span>创建服务器实例</span></li><li><span>给服务器实例绑定事件，使用.on进行事件的监听，服务器实例返回回调函数，回调函数两个参数是req和res，是请求对象和响应对象。</span></li><li><span>启动服务器，参数有一个对调函数</span></li></ul><p><span>注意：给服务器绑定事件的回调函数和启动服务器时的回调函数时不一样的。</span></p><p><img src="Node.js.assets/image-20220314165124284.png" referrerpolicy="no-referrer" alt="image-20220314165124284"></p><h3><a name="req请求对象" class="md-header-anchor"></a><span>req请求对象</span></h3><p><img src="Node.js.assets/image-20220314165536592.png" referrerpolicy="no-referrer" alt="image-20220314165536592"></p><p><span>知识点复习</span></p><p><span>js的字符串拼接，用tab键上的反引号代替引号，是ES6中新增的语法，通过</span><span>$</span><span>可以直接引用变量的值，称为模板字符串</span></p><h3><a name="res响应对象" class="md-header-anchor"></a><span>res响应对象</span></h3><p><span>使用res.end方法，给客户端响应一些内容。中文乱码可设置响应头为utf8。res.writeHead(200,{&#39;Content-Type&#39;:&#39;text/html; charset=utf-8&#39;})</span></p><p><span>或者是使用setHead方法</span></p><p><img src="Node.js.assets/image-20220314170042041.png" referrerpolicy="no-referrer" alt="image-20220314170042041"></p><p><img src="Node.js.assets/image-20220314170314259.png" referrerpolicy="no-referrer" alt="image-20220314170314259"></p><h2><a name="路由使用" class="md-header-anchor"></a><span>路由使用</span></h2><p><span>根据不同的url，响应不同的内容</span></p><p><span>步骤</span></p><p><img src="Node.js.assets/image-20220314170804566.png" referrerpolicy="no-referrer" alt="image-20220314170804566"></p><p><span>代码实现</span></p><p><img src="Node.js.assets/image-20220314170905933.png" referrerpolicy="no-referrer" alt="image-20220314170905933"></p><h2><a name="案例-实现资源的返回" class="md-header-anchor"></a><span>案例 实现资源的返回</span></h2><p><img src="Node.js.assets/image-20220314171435494.png" referrerpolicy="no-referrer" alt="image-20220314171435494"></p><p><span>此时的服务器软件相当于资源的搬运工，将本地的资源读进内存中，然后将数据发送到客户端。资源就是和一个一个的url进行绑定的，所以URI和URL的表示很多时候就是指某一个资源的位置，统一资源定位符</span></p><p><span>客户端有关的信息都是在请求中携带过来的，获取客户端的信息（不是客户），一般都是在请求中获取，因为请求中就会带有客户端访问的信息。</span></p><h1><a name="模块化" class="md-header-anchor"></a><span>模块化</span></h1><p><span>默认使用的是commonJs规范，使用的是 require和module.export语法</span></p><p><img src="Node.js.assets/image-20220314174546862.png" referrerpolicy="no-referrer" alt="image-20220314174546862"></p><h2><a name="模块分类" class="md-header-anchor"></a><span>模块分类</span></h2><p><img src="Node.js.assets/image-20220314174809993.png" referrerpolicy="no-referrer" alt="image-20220314174809993"></p><h2><a name="模块加载" class="md-header-anchor"></a><span>模块加载</span></h2><p><img src="Node.js.assets/image-20220314174909698.png" referrerpolicy="no-referrer" alt="image-20220314174909698"></p><p><span>注意加载自定义模块的使用是导入一个路径，而不是一个名称，当然也可以变为一个名称，知识需要安装。在默认路径下进行导报。</span></p><p><span>注意：再使用require方法对其他模块进行加载时，会自动执行其他模块内的方法。可以省略js的拓展名。</span></p><h2><a name="模块作用域" class="md-header-anchor"></a><span>模块作用域</span></h2><p><span>如果属性或者是定义的数据没有被导出，那么其他模块是不能访问整个模块内的属性的，类似于Java开发中的pojo，或者是一个bean，提供了private私有属性，但是提供了setget方法进行访问。</span></p><p><img src="Node.js.assets/image-20220314175420457.png" referrerpolicy="no-referrer" alt="image-20220314175420457"></p><h2><a name="module对象" class="md-header-anchor"></a><span>module对象</span></h2><p><span>每一个js模块都有一个module对象指向，存储了当前模块有关的信息。</span></p><p><img src="Node.js.assets/image-20220314175903361.png" referrerpolicy="no-referrer" alt="image-20220314175903361"></p><p>&nbsp;</p><p><span>包括当前的路径，文件名称，父子模块的关系等</span></p><p><img src="Node.js.assets/image-20220314180059390.png" referrerpolicy="no-referrer" alt="image-20220314180059390"></p><p>&nbsp;</p><p><span>一定要记住，使用require进行模块导入时，会自动执行模块内的函数，执行被导入模块内的函数。之所以能够导入模块内的对象，是因为module.export中定义了导出的对象。所以用来接收模块的对象默认时空的。</span></p><p><img src="Node.js.assets/image-20220314180458802.png" referrerpolicy="no-referrer" alt="image-20220314180458802"></p><p><span>在commonjs语法中，许多都是module.开头的使用，是因为commonjs默认每一个js文件就是一个module，访问一个js文件就是访问一个module对象。所以很多时候都是module进行属性或者是方法的访问。</span></p><p><span>直接导出一个属性</span></p><p><img src="Node.js.assets/image-20220314180728922.png" referrerpolicy="no-referrer" alt="image-20220314180728922"></p><p><span>或者是定义了一个私有变量后，进行导出</span></p><p><img src="Node.js.assets/image-20220314180844806.png" referrerpolicy="no-referrer" alt="image-20220314180844806"></p><h2><a name="注意点" class="md-header-anchor"></a><span>注意点</span></h2><p><img src="Node.js.assets/image-20220314180900602.png" referrerpolicy="no-referrer" alt="image-20220314180900602"></p><p><span>即类似于层叠行，后面的代码会将前面已有的属性覆盖。</span></p><p><span>有些写法只是指定了一个属性，所以不会发生覆盖的情况，但是当整个module.export指向新的对象时，原有的即使是单独的设置属性，那也会覆盖掉原有的单独设置的属性。</span></p><p><span>一定要注意：导入的结果，永远都是以module.export指向的对象为准。</span></p><h2><a name="exports对象" class="md-header-anchor"></a><span>exports对象</span></h2><p><img src="Node.js.assets/image-20220314181935842.png" referrerpolicy="no-referrer" alt="image-20220314181935842"></p><h2><a name="注意" class="md-header-anchor"></a><span>注意</span></h2><p><img src="Node.js.assets/image-20220314182222633.png" referrerpolicy="no-referrer" alt="image-20220314182222633"></p><p><span>应该是nodejs底层没有对exports直接赋值作相应处理。</span></p><p><span>module.export和export有优先级的关系，export的优先级比较低</span></p><p><img src="Node.js.assets/image-20220314182559727.png" referrerpolicy="no-referrer" alt="image-20220314182559727"></p><p><span>看是否指向新对象，也看是否是单独设置属性，但记住require中导入的一定是module.export对象</span></p><h2><a name="规范" class="md-header-anchor"></a><span>规范</span></h2><p><img src="Node.js.assets/image-20220314182821205.png" referrerpolicy="no-referrer" alt="image-20220314182821205"></p><h1><a name="包" class="md-header-anchor"></a><span>包</span></h1><h3><a name="包的语义化规范" class="md-header-anchor"></a><span>包的语义化规范</span></h3><p><img src="Node.js.assets/image-20220314195516990.png" referrerpolicy="no-referrer" alt="image-20220314195516990"></p><h3><a name="包解释" class="md-header-anchor"></a><span>包解释</span></h3><p><img src="Node.js.assets/image-20220314182927000.png" referrerpolicy="no-referrer" alt="image-20220314182927000"></p><h2><a name="包的安装使用" class="md-header-anchor"></a><span>包的安装使用</span></h2><p><img src="Node.js.assets/image-20220314183018704.png" referrerpolicy="no-referrer" alt="image-20220314183018704"></p><h3><a name="时间格式化" class="md-header-anchor"></a><span>时间格式化</span></h3><p><img src="Node.js.assets/image-20220314194606818.png" referrerpolicy="no-referrer" alt="image-20220314194606818"></p><p><span>现在推荐用Day.js代替moment.js</span></p><p><span>现在推荐使用Day.js代替moment.js，因为moment.js的打包体积太大了。</span></p><p><img src="Node.js.assets/image-20220314195352221.png" referrerpolicy="no-referrer" alt="image-20220314195352221"></p><h2><a name="目录" class="md-header-anchor"></a><span>目录</span></h2><p><img src="Node.js.assets/image-20220314195329918.png" referrerpolicy="no-referrer" alt="image-20220314195329918"></p><h3><a name="包管理" class="md-header-anchor"></a><span>包管理</span></h3><p><img src="Node.js.assets/image-20220314195712897.png" referrerpolicy="no-referrer" alt="image-20220314195712897"></p><p><span>一定要将 node_module添加到 .gitigonre中，方便上传，并且别人再使用的时候直接下载 npm install 即可防止环境的不同导致各种意想不到的错误</span></p><p><img src="Node.js.assets/image-20220314200010781.png" referrerpolicy="no-referrer" alt="image-20220314200010781"></p><p>&nbsp;</p><h3><a name="包管理配置文件" class="md-header-anchor"></a><span>包管理配置文件</span></h3><p><img src="Node.js.assets/image-20220314200049310.png" referrerpolicy="no-referrer" alt="image-20220314200049310"></p><h3><a name="卸载包" class="md-header-anchor"></a><span>卸载包</span></h3><p><img src="Node.js.assets/image-20220314200409453.png" referrerpolicy="no-referrer" alt="image-20220314200409453"></p><p>&nbsp;</p><h3><a name="运行时和打包编译时" class="md-header-anchor"></a><span>运行时和打包编译时</span></h3><p><img src="Node.js.assets/image-20220314200511563.png" referrerpolicy="no-referrer" alt="image-20220314200511563"></p><h2><a name="包的分类" class="md-header-anchor"></a><span>包的分类</span></h2><p><span>项目包</span></p><p><img src="Node.js.assets/image-20220314200835207.png" referrerpolicy="no-referrer" alt="image-20220314200835207"></p><p><span>全局包</span></p><p><img src="Node.js.assets/image-20220314200920236.png" referrerpolicy="no-referrer" alt="image-20220314200920236"></p><p>&nbsp;</p><h3><a name="全局包实例" class="md-header-anchor"></a><span>全局包实例</span></h3><p><span>使用全举报的好处是只要加载了包，那么在本机上任何地方都可以使用，但是项目包只能在项目中进行使用，对于全局包一般是常用的脚手架工具，或者是一些转换的工具类，没有版本依赖问题的就作为全局包，项目包是为了解决有些项目只能泡在固定的版本上，如果版本不一致就会引发各种问题，此时就必须要提供相同的运行环境。</span></p><p><span>i5ting_toc能够将md文档转换为html文件</span></p><h2><a name="规范的包" class="md-header-anchor"></a><span>规范的包</span></h2><p><img src="Node.js.assets/image-20220314201956919.png" referrerpolicy="no-referrer" alt="image-20220314201956919"></p><p><span>###包结构</span></p><p><strong><img src="Node.js.assets/image-20220314202234287.png" referrerpolicy="no-referrer" alt="image-20220314202234287"></strong></p><p>&nbsp;</p><p><span>开发目录</span></p><p><img src="Node.js.assets/image-20220314203554133.png" referrerpolicy="no-referrer" alt="image-20220314203554133"></p><h1><a name="模块的加载机制" class="md-header-anchor"></a><span>模块的加载机制</span></h1><ul><li><span>模块加载会被缓存</span></li></ul><p><img src="Node.js.assets/image-20220314204226295.png" referrerpolicy="no-referrer" alt="image-20220314204226295"></p><ul><li><span>内置的模块包优先级最高</span></li></ul><p><img src="Node.js.assets/image-20220314204324356.png" referrerpolicy="no-referrer" alt="image-20220314204324356"></p><ul><li><span>加载自定义的模块，必须要制定文件的路径，相对路径，否则会按照相对路径进行寻找</span></li></ul><p><img src="Node.js.assets/image-20220314204432705.png" referrerpolicy="no-referrer" alt="image-20220314204432705"></p><ul><li><span>记载模块时，吐过省略了文件的拓展名，那么会按照以下的拓展名进行寻找</span></li></ul><p><img src="Node.js.assets/image-20220314204552568.png" referrerpolicy="no-referrer" alt="image-20220314204552568"></p><ul><li><span>当给定的一个模块没有给定相对路径时，即先从当前的node_module进行寻找，如果没有找到，那么会往上一级进行查找，直至找到或者是找不到。</span></li></ul><p><img src="Node.js.assets/image-20220314204726264.png" referrerpolicy="no-referrer" alt="image-20220314204726264"></p><ul><li><span>目录作为模块进行加载</span></li></ul><p><img src="Node.js.assets/image-20220314204922863.png" referrerpolicy="no-referrer" alt="image-20220314204922863"></p><h1><a name="express" class="md-header-anchor"></a><span>Express</span></h1><h2><a name="简介" class="md-header-anchor"></a><span>简介</span></h2><p><span>是一个基于Nodejs的快速开发web模块，能够提供快速的web开发项目实现</span></p><p><img src="Node.js.assets/image-20220314205216352.png" referrerpolicy="no-referrer" alt="image-20220314205216352"></p><h2><a name="理解" class="md-header-anchor"></a><span>理解</span></h2><p><img src="Node.js.assets/image-20220314205249570.png" referrerpolicy="no-referrer" alt="image-20220314205249570"></p><p><span>开发的效率更高， 提供的服务更多。</span></p><h3><a name="功能" class="md-header-anchor"></a><span>功能</span></h3><p><span>一个是直接作为web服务器进行服务，一个是API接口服务器，一般都是前后端项目的服务，即提供接口进行服务</span></p><p><img src="Node.js.assets/image-20220314205342902.png" referrerpolicy="no-referrer" alt="image-20220314205342902"></p><p>&nbsp;</p><p><span>一般固定某一个版本的express</span></p><p><img src="Node.js.assets/image-20220314205527906.png" referrerpolicy="no-referrer" alt="image-20220314205527906"></p><p><span>express()是无参构造函数，创建一个express实例</span></p><h3><a name="使用" class="md-header-anchor"></a><span>使用</span></h3><p><span>三个步骤创建express服务器</span></p><p><img src="Node.js.assets/image-20220314205623884.png" referrerpolicy="no-referrer" alt="image-20220314205623884"></p><p><span>一般node中使用模块的步骤都是</span></p><ul><li><span>导入模块</span></li><li><span>使用模块创建实例</span></li><li><span>使用实例，或者是启动实例</span></li></ul><h2><a name="使用-n610" class="md-header-anchor"></a><span>使用</span></h2><p><span>和原生http模块不一样的是，express模块导入可以生成一个服务器，然后创建一个实例，最后绑定监听的端口即可</span></p><h3><a name="监听get请求" class="md-header-anchor"></a><span>监听get请求</span></h3><p><span>和原生的http模块类似，第一个参数是url</span></p><p><img src="Node.js.assets/image-20220314210021394.png" referrerpolicy="no-referrer" alt="image-20220314210021394"></p><h3><a name="监听post请求" class="md-header-anchor"></a><span>监听post请求</span></h3><p><span>和get请求类似，第一个是url，第二个参数是回调函数，回调函数的参数是两个，和原生的http相似，第一个参数是请求对象，第二个参数是响应对象</span></p><p><img src="Node.js.assets/image-20220314210043110.png" referrerpolicy="no-referrer" alt="image-20220314210043110"></p><h3><a name="响应客户端" class="md-header-anchor"></a><span>响应客户端</span></h3><p><span>类似于原生的node的http模块中的end方法，express提供的是send方法，将处理好的内容提供给客户端。</span></p><p><img src="Node.js.assets/image-20220314210144361.png" referrerpolicy="no-referrer" alt="image-20220314210144361"></p><p><span>，express服务模块是前端开发中经常使用的简单服务器的框架，主要是创建服务实例或者是提供api接口简单，能够快速的搭建起一个服务实例，所以一定要掌握好基于nodejs的服务实例，express模块。</span></p><ul><li><span>app.get(url,function(req,res){})</span></li><li><span>app.post(url,function(req,res){})</span></li><li><span>app.send(val)</span></li></ul><h2><a name="参数查询" class="md-header-anchor"></a><span>参数查询</span></h2><h3><a name="url中的参数查询" class="md-header-anchor"></a><span>url中的参数查询</span></h3><p><span>默认情况是：query是一个空对象，既没有参数，通过rea.query可以获得url中的参数</span></p><p><img src="Node.js.assets/image-20220314211058042.png" referrerpolicy="no-referrer" alt="image-20220314211058042"></p><h3><a name="url中的动态参数" class="md-header-anchor"></a><span>url中的动态参数</span></h3><p><span>动态参数简单来理解就是RESTful风格的url，但是url中的参数不确定</span></p><p><img src="Node.js.assets/image-20220314211910169.png" referrerpolicy="no-referrer" alt="image-20220314211910169"></p><p><span>rea.params获取的，是给定的路由中，：冒号后面的id就是key，实际传递的值就是value，简单来说就是 冒号后面的就是params的key，是一个变化的key，实际传递的值就是一个value，只只有冒号是固定的写法，其余的都不是固定的写法。可以有多个参数的写法，即能够匹配动态参数。注意这个即使是url参数，但是和query参数不一样的。</span></p><h2><a name="静态资源管理" class="md-header-anchor"></a><span>静态资源管理</span></h2><ul><li><span>普通静态资源管理</span></li><li><span>多个静态资源存储管理</span></li><li><span>给定前缀的静态资源管理</span></li><li><span>多个给定静态资源管理</span></li></ul><p><span>express提供的静态资源服务中的express.static()函数不是app.static()而是直接俄调用express.static()，就能够将某个文件夹作为静态资源文件夹了，此时需要注意的是：在访问时url中不会出现指定的静态资源文件夹，因为express是通过映射的形式制定了静态资源文件夹，类似于Django的static </span></p><p><img src="Node.js.assets/image-20220314215814051.png" referrerpolicy="no-referrer" alt="image-20220314215814051"></p><h3><a name="提供服务" class="md-header-anchor"></a><span>提供服务</span></h3><p><img src="Node.js.assets/image-20220314221541380.png" referrerpolicy="no-referrer" alt="image-20220314221541380"></p><p><span>通过app.use(rexpress.static(路径)) 即可完成静态资源的提供服务。在外层使用app.use()，绑定上当前的服务实例，然后对静态资源的目录进行指定，通过express.static()中指定的静态资源文件存储位置。</span></p><h3><a name="多个静态资源文件存储位置" class="md-header-anchor"></a><span>多个静态资源文件存储位置</span></h3><p><span>express会根据托管的静态资源顺序查找，按照静态资源托管的顺序进行顺序的查找。查到了这个静态资源文件，那么就不会继续往下寻找，如果找不到就会报404的错误，只是寻找符合要求的第一个静态资源文件，按照静态资源托管的顺序进行查找。</span></p><p><img src="Node.js.assets/image-20220314221836616.png" referrerpolicy="no-referrer" alt="image-20220314221836616"></p><p>&nbsp;</p><h3><a name="挂载路径前缀" class="md-header-anchor"></a><span>挂载路径前缀</span></h3><p><span>即url中要按照给定的前缀才能访问给定的静态资源文件，否则会找不到资源文件。</span></p><p><img src="Node.js.assets/image-20220314222245911.png" referrerpolicy="no-referrer" alt="image-20220314222245911"></p><h2><a name="nodemon" class="md-header-anchor"></a><span>nodemon</span></h2><h3><a name="解释" class="md-header-anchor"></a><span>解释</span></h3><p><img src="Node.js.assets/image-20220314222544820.png" referrerpolicy="no-referrer" alt="image-20220314222544820"></p><h3><a name="使用-n662" class="md-header-anchor"></a><span>使用</span></h3><p><img src="Node.js.assets/image-20220314222717003.png" referrerpolicy="no-referrer" alt="image-20220314222717003"></p><h2><a name="express路由" class="md-header-anchor"></a><span>Express路由</span></h2><h3><a name="路由" class="md-header-anchor"></a><span>路由</span></h3><p><span>路由是一种映射关系，能够将某一个东西与另外一个东西关联起来。</span></p><p><span>在服务过程中，路由就是客户端的请求和服务端处理函数的映射关系。</span></p><p><img src="Node.js.assets/image-20220314223233700.png" referrerpolicy="no-referrer" alt="image-20220314223233700"></p><h3><a name="案例-n669" class="md-header-anchor"></a><span>案例</span></h3><p><img src="Node.js.assets/image-20220314223331097.png" referrerpolicy="no-referrer" alt="image-20220314223331097"></p><h3><a name="路由匹配过程" class="md-header-anchor"></a><span>路由匹配过程</span></h3><p><img src="Node.js.assets/image-20220314224513686.png" referrerpolicy="no-referrer" alt="image-20220314224513686"></p><p>&nbsp;</p><p><img src="Node.js.assets/image-20220314224859537.png" referrerpolicy="no-referrer" alt="image-20220314224859537"></p><p>&nbsp;</p><pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="javascript"><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang="javascript"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 7px; left: 34px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 26px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre><div class="CodeMirror-linenumber CodeMirror-gutter-elt"><div>4</div></div></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation"><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: -26px; width: 26px;"></div><div class="CodeMirror-gutter-wrapper CodeMirror-activeline-gutter" style="left: -26px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 17px;">1</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">最简单的路由实现</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -26px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 17px;">2</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">app</span>.<span class="cm-property">get</span>(<span class="cm-string">'/'</span>,(<span class="cm-def">req</span>,<span class="cm-def">res</span>) <span class="cm-operator">=&gt;</span> {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -26px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 17px;">3</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp;<span class="cm-variable">app</span>.<span class="cm-property">send</span>(<span class="cm-string">"世界你好"</span>)</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -26px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 17px;">4</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">})</span></pre></div></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom: 0px solid transparent; top: 90px;"></div><div class="CodeMirror-gutters" style="height: 90px;"><div class="CodeMirror-gutter CodeMirror-linenumbers" style="width: 25px;"></div></div></div></div></pre><h3><a name="模块化路由" class="md-header-anchor"></a><span>模块化路由</span></h3><p><img src="Node.js.assets/image-20220314225053514.png" referrerpolicy="no-referrer" alt="image-20220314225053514"></p><h3><a name="创建过程" class="md-header-anchor"></a><span>创建过程</span></h3><p><img src="Node.js.assets/image-20220314225201856.png" referrerpolicy="no-referrer" alt="image-20220314225201856"></p><p><span>理解js中的解构 </span></p><pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="javascript"><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang="javascript"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 7px; left: 41px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 33px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre><div class="CodeMirror-linenumber CodeMirror-gutter-elt"><div>10</div></div></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: -33px; width: 33px;"></div><div class="CodeMirror-gutter-wrapper CodeMirror-activeline-gutter" style="left: -33px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 24px;">1</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-keyword">import</span> {<span class="cm-def">xxx</span>,<span class="cm-def">xxx</span>} <span class="cm-keyword">from</span> <span class="cm-string">'xxx.js'</span> <span class="cm-variable">这个就是一个解构，简单来说从js文件中拿到了两个对象，虽然看起来是挺正常的，但是这个就是一个解构</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -33px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 24px;">2</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -33px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 24px;">3</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">一个参数中是一个对象</span> <span class="cm-variable">context</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -33px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 24px;">4</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">context</span> <span class="cm-operator">=</span> {</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -33px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 24px;">5</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp;<span class="cm-property">age</span>: <span class="cm-number">18</span>,</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -33px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 24px;">6</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &nbsp;<span class="cm-property">name</span>: <span class="cm-variable">lisi</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -33px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 24px;">7</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">}</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -33px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 24px;">8</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -33px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 24px;">9</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">如果需要解构对象，除了可以直接使用</span> <span class="cm-meta">...</span> <span class="cm-variable">拓展运算符号外，还可以直接拿到独享中的属性</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -33px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 24px;">10</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">如</span> {<span class="cm-variable">age</span>} <span class="cm-variable">就是一个结构对象的过程，注意使用这个语法，必须是原context中的位置，</span>{<span class="cm-variable">age</span>}<span class="cm-variable">代替了context的参数位置，这样的解构才是有效果的。</span></span></pre></div></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom: 0px solid transparent; top: 269px;"></div><div class="CodeMirror-gutters" style="height: 269px;"><div class="CodeMirror-gutter CodeMirror-linenumbers" style="width: 32px;"></div></div></div></div></pre><h3><a name="使用路由对象" class="md-header-anchor"></a><span>使用路由对象</span></h3><p><img src="Node.js.assets/image-20220314225918211.png" referrerpolicy="no-referrer" alt="image-20220314225918211"></p><p><img src="Node.js.assets/image-20220314230000865.png" referrerpolicy="no-referrer" alt="image-20220314230000865"></p><h3><a name="添加路由模块的前缀" class="md-header-anchor"></a><span>添加路由模块的前缀</span></h3><p><img src="Node.js.assets/image-20220314230258556.png" referrerpolicy="no-referrer" alt="image-20220314230258556"></p><p><span>即url需要特定的访问前缀才能正确的访问到响应的服务，否则404</span></p><h2><a name="阶段总结" class="md-header-anchor"></a><span>阶段总结</span></h2><h3><a name="基础部分" class="md-header-anchor"></a><span>基础部分</span></h3><h3><a name="express部分" class="md-header-anchor"></a><span>Express部分</span></h3><p><span>在node中，虽然可以有简单的http模块能够简单的进行服务通信，但是原生的api过于简单，服务过于单一，所以出现了express这个框架，能够帮助我们快速的搭建一个简单的响应接口或者是web服务。</span></p><ul><li><span>导入express</span></li><li><span>调用express()函数，返回一个服务实例app（任意名）</span></li><li><span>此时的app就是一个服务实例了，能够进行服务的通信了</span></li><li><span>启动express服务实例，使用app.method，就能够完成对应的请求方式的处理，启动express的服务使用的是app.listten(端口号，启动的回调函数)</span></li><li><span>这些函数都有两个参数，第一个参数是url，第二个参数是一个回调函数，回调函数有两个参数，第一个参数是请求对象req，第二个参数是响应对象res，</span></li><li><span>express框架使用app.send方法，将处理好的内容返回客户端</span></li><li><span>express调用req的属性，能够将url或者reqbody中的参数拿到，默认情况下这些都是一个空的对象。</span></li><li><span>express.static()能够将某一个存储位置变为一个静态资源管理位置，即能够放行其中的内容不需要经过监听拦截等。静态资源管理由普通的静态资源管理，多个普通静态资源管理，给定前缀静态资源管理，多个给定前缀静态资源管理等等。注意：使用express.static()函数必须是express.static()作为一个参数传入 app.ues() 中，表示当前的服务实例绑定了这个静态资源管理，如果只有一个参数是express.static()，那么表示不给顶前缀，如果第一个参数是一个字符串，表示当前的静态资源文件管理的访问中，url必须要带有给定的前缀才能够访问，否则不允许通过，或者是响应404</span></li></ul><h2><a name="express中间件" class="md-header-anchor"></a><span>Express中间件</span></h2><h3><a name="简介-n716" class="md-header-anchor"></a><span>简介</span></h3><p><span>中间件简单来说是为了提升开发效率，将一些复用的流程抽离出来，然后按照网络的请求处理过程进行调用。</span></p><p><img src="Node.js.assets/image-20220315075606847.png" referrerpolicy="no-referrer" alt="image-20220315075606847"></p><h3><a name="格式" class="md-header-anchor"></a><span>格式</span></h3><p><span>如何区分一个路由函数和中间件函数，中间件中next函数是必选的参数。路由函数中只有只有req和res</span></p><p><img src="Node.js.assets/image-20220315075643225.png" referrerpolicy="no-referrer" alt="image-20220315075643225"></p><h3><a name="定义中间件函数" class="md-header-anchor"></a><span>定义中间件函数</span></h3><p><img src="Node.js.assets/image-20220315080010544.png" referrerpolicy="no-referrer" alt="image-20220315080010544"></p><p><span>一定要调用next函数，next（）相当于通行证，如果没有调用next函数，那么这次的网络请求就会停留在这个中间件中，无法继续做出相应。</span></p><p><span>请求的对象一般是先进入中间件处理，最后才是路由函数的处理，简单来说中间件就是处理数据的流程，调用next函数进行多次的数据处理，最后调用了路由函数。一定要记住没有next函数，就会被一直拦截，无法响应</span></p><h4><a name="全局中间件" class="md-header-anchor"></a><span>全局中间件</span></h4><p><img src="Node.js.assets/image-20220315080450242.png" referrerpolicy="no-referrer" alt="image-20220315080450242"></p><p><img src="Node.js.assets/image-20220315080756013.png" referrerpolicy="no-referrer" alt="image-20220315080756013"></p><h4><a name="简化中间件的写法" class="md-header-anchor"></a><span>简化中间件的写法</span></h4><p><img src="Node.js.assets/image-20220315080924061.png" referrerpolicy="no-referrer" alt="image-20220315080924061"></p><h3><a name="中间件的作用" class="md-header-anchor"></a><span>中间件的作用</span></h3><p><span>对于一个请求响应对象来说来说，多个中间件是共享这次的这次的请求响应对象的，所以可以在上游的中间件中添加相应的属性供下游的中间件使用。简单说就是不同的路由中相同的处理流程抽离出来降低复杂度，冗余度</span></p><p><img src="Node.js.assets/image-20220315081021118.png" referrerpolicy="no-referrer" alt="image-20220315081021118"></p><p><img src="Node.js.assets/image-20220315081517458.png" referrerpolicy="no-referrer" alt="image-20220315081517458"></p><h3><a name="定义多个全局中间件" class="md-header-anchor"></a><span>定义多个全局中间件</span></h3><p><span>会按照定义的顺序进行顺序的执行所有的全局中间件，最后返回到路由函数中，进行响应。#</span></p><p><img src="Node.js.assets/image-20220315081700643.png" referrerpolicy="no-referrer" alt="image-20220315081700643"></p><p><img src="Node.js.assets/image-20220315081718577.png" referrerpolicy="no-referrer" alt="image-20220315081718577"></p><h3><a name="局部中间件" class="md-header-anchor"></a><span>局部中间件</span></h3><p><span>在路由函数中，第二个参数的位置写上自定义的中间件名称，即可仅对当前路由生效，不影响其他的路由。</span></p><p><img src="Node.js.assets/image-20220315081859912.png" referrerpolicy="no-referrer" alt="image-20220315081859912"></p><h3><a name="使用多个局部中间件" class="md-header-anchor"></a><span>使用多个局部中间件</span></h3><p><img src="Node.js.assets/image-20220315082144228.png" referrerpolicy="no-referrer" alt="image-20220315082144228"></p><p><span>在第二个参数的位置上，调用多个中间叫你函数即可，或者是多个中间件作为数组传入第二个参数，会按照给定的顺序进行处理。会按照给定的参数的位置或者是数组中的位置进行处理。</span></p><h3><a name="注意事项" class="md-header-anchor"></a><span>注意事项</span></h3><p><span>由于js是顺序执行，从上到下执行的流程，所以定义中间件要在路由函数之前，或者是导入中间件函数，要在路由函数之前。一定要next函数，多个中间件是共享一次的请求对象和响应对象的。</span></p><p><img src="Node.js.assets/image-20220315082433141.png" referrerpolicy="no-referrer" alt="image-20220315082433141"></p><h3><a name="中间件分类" class="md-header-anchor"></a><span>中间件分类</span></h3><p><img src="Node.js.assets/image-20220315082556133.png" referrerpolicy="no-referrer" alt="image-20220315082556133"></p><ul><li><span>应用级中间件- 绑定到了app上</span></li></ul><p><img src="Node.js.assets/image-20220315082633639.png" referrerpolicy="no-referrer" alt="image-20220315082633639"></p><ul><li><span>路由级别的中间件- 绑定到路由上</span></li></ul><p><img src="Node.js.assets/image-20220315082659322.png" referrerpolicy="no-referrer" alt="image-20220315082659322"></p><ul><li><span>错误级别的中间件函数- 捕获异常的中间件函数</span></li></ul><p><img src="Node.js.assets/image-20220315083509513.png" referrerpolicy="no-referrer" alt="image-20220315083509513"></p><p><span>捕获异常的中间件函数一般写在路由的后面，只要发生了错了就会调用错误级别的中间件函数，错误级别的中间件函数有四个参数，第一个参数 就是err对象，其余参数和其他的中间件参数一致，同时也是共享一次请求响应对象的。</span></p><p><span>错误中间件必须写在左右中间件以及路由的后满。是按照js执行流程来决定的，其他的全局局部中间件需要在路由函数之前起作用所以写在路由函数之前，而错误级别的中间件是捕获全局的异常，所以写在所有路由函数之后，才能捕获所有的错误。</span></p><ul><li><span>Express内置的中间件</span></li></ul><p><span>最常使用的三个中间件就是静态资源的管理（回顾static的写法）</span></p><pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="js"><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang="js"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 7px; left: 34px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 26px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre>x</pre></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: -26px; width: 26px;"></div><div class="CodeMirror-gutter-wrapper CodeMirror-activeline-gutter" style="left: -26px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 17px;">1</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">app</span>.<span class="cm-property">use</span>(<span class="cm-variable">express</span>.<span class="cm-property">static</span>(<span class="cm-string">'静态资源目录地址'</span>))</span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -26px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 17px;">2</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -26px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 17px;">3</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">或者是</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -26px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 0px; width: 17px;">4</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre></div><div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -26px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt CodeMirror-linenumber-show" style="left: 0px; width: 17px;">5</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">app</span>.<span class="cm-property">use</span>(<span class="cm-string">'前缀别名'</span>,<span class="cm-variable">express</span>.<span class="cm-property">static</span>(<span class="cm-string">'静态资源目录地址'</span>))</span></pre></div></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom: 0px solid transparent; top: 112px;"></div><div class="CodeMirror-gutters" style="height: 112px; left: 0px;"><div class="CodeMirror-gutter CodeMirror-linenumbers" style="width: 25px;"></div></div></div></div></pre><p>&nbsp;</p><p><img src="Node.js.assets/image-20220315083742150.png" referrerpolicy="no-referrer" alt="image-20220315083742150"></p><ul><li><span>第三方的中间件</span></li></ul><p><img src="Node.js.assets/image-20220315090025519.png" referrerpolicy="no-referrer" alt="image-20220315090025519"></p><p>&nbsp;</p><h3><a name="自定义中间件" class="md-header-anchor"></a><span>自定义中间件</span></h3><p><img src="Node.js.assets/image-20220315090148596.png" referrerpolicy="no-referrer" alt="image-20220315090148596"></p><h3><a name="监听data事件" class="md-header-anchor"></a><span>监听data事件</span></h3><p><img src="Node.js.assets/image-20220315090506025.png" referrerpolicy="no-referrer" alt="image-20220315090506025"></p><h3><a name="监听end事件" class="md-header-anchor"></a><span>监听end事件</span></h3><p><span>可以学习TCP/IP中怎么结束一个传输过程，传送的信息是什么，每一门语言都提供了响应的处理函数。</span></p><p><img src="Node.js.assets/image-20220315090949375.png" referrerpolicy="no-referrer" alt="image-20220315090949375"></p><h2><a name="跨域问题" class="md-header-anchor"></a><span>跨域问题</span></h2><p><img src="Node.js.assets/image-20220315115846942.png" referrerpolicy="no-referrer" alt="image-20220315115846942"></p><h3><a name="express解决跨域问题" class="md-header-anchor"></a><span>express解决跨域问题</span></h3><p><span>使用一个中间件允许客户端进行跨域。跨域的解决有纯前端跨域和后端解决两种方案，跨域是相对于客户端访问服务端时的流程来说的，对于一个服务端来说，如果检测到了客户端访问时，浏览器会自动进行跨域的拦截，如果检测到了协议ip或者是端口号有一个不同，那么就会引发跨域问题，这是仅限于客户端访问服务端才会出现，如果是服务器请求另外一个服务不会出现跨域问题。这个是浏览器自带的安全策略。</span></p><ul><li><span>前端使用的就是模拟一个服务器发送请求，对真实的请求进行代理，让模拟的服务器发送请求后将数据转给本地的数据渲染框架。</span></li><li><span>后端的解决方案很简单，允许客户端跨域即可</span></li></ul><p><img src="Node.js.assets/image-20220315115944535.png" referrerpolicy="no-referrer" alt="image-20220315115944535"></p><p><img src="Node.js.assets/image-20220315120709127.png" referrerpolicy="no-referrer" alt="image-20220315120709127"></p><p>&nbsp;</p><h3><a name="解决方案-n880" class="md-header-anchor"></a><span>解决方案</span></h3><p><span>第一个</span></p><p><img src="Node.js.assets/image-20220315121011692.png" referrerpolicy="no-referrer" alt="image-20220315121011692"></p><p><span>第二个</span></p><p><img src="Node.js.assets/image-20220315121118120.png" referrerpolicy="no-referrer" alt="image-20220315121118120"></p><p><span>第三个</span></p><p><img src="Node.js.assets/image-20220315121137525.png" referrerpolicy="no-referrer" alt="image-20220315121137525"></p><h2><a name="请求" class="md-header-anchor"></a><span>请求</span></h2><p><span>OPTIONS就是预检请求，检查请求方式的种类有什么，进行校验，然后判断能否发送正式请求</span></p><p><span>普通请求</span></p><p><img src="Node.js.assets/image-20220315121542133.png" referrerpolicy="no-referrer" alt="image-20220315121542133"></p><p><span>预检请求</span></p><p><img src="Node.js.assets/image-20220315121332354.png" referrerpolicy="no-referrer" alt="image-20220315121332354"></p><h3><a name="jsonp" class="md-header-anchor"></a><span>JSONP</span></h3><p><span>JSONP相当于后台生成一段JS代码插入到前端JS页面中，来执行前端已经定义好的回调函数</span></p><p><span>后面因为使用JQ封装的JSONP，用起来和GET/POST一样，但是其实里面根本不一样</span></p><p><img src="Node.js.assets/image-20220315121759028.png" referrerpolicy="no-referrer" alt="image-20220315121759028"></p><h2><a name="数据库" class="md-header-anchor"></a><span>数据库</span></h2><p><img src="Node.js.assets/image-20220315122824411.png" referrerpolicy="no-referrer" alt="image-20220315122824411"></p><h3><a name="安装" class="md-header-anchor"></a><span>安装</span></h3><p><img src="Node.js.assets/image-20220315122921849.png" referrerpolicy="no-referrer" alt="image-20220315122921849"></p><h3><a name="配置模块" class="md-header-anchor"></a><span>配置模块</span></h3><ul><li><span>导入模块</span></li><li><span>创建数据库服务实例，进行配置</span></li></ul><p><img src="Node.js.assets/image-20220315122943644.png" referrerpolicy="no-referrer" alt="image-20220315122943644"></p><h3><a name="使用-n938" class="md-header-anchor"></a><span>使用</span></h3><ul><li><span>查询语句</span></li></ul><ul><li><span>使用数据库服务实例调用query函数即可，</span></li></ul><p><span>直接使用db.query进行查询即可，有一个回调函数，第一个蚕食SQL语句</span></p><p><span>第一个参数是查询语句，第二个参数是回调函数，回调函数传入两个参数，第一个是err，第二个是res，记住node的设计是错误先行。和Java等不一样的还需要配置bean，pojo等进行映射关系的实体类等，js中直接可以生成对象，并且数据类型并不是固定的，所以在进行数据的操作时，很容易的就实现了数据的封装。这个也是弱类型语言的优势，适合作为一个orm框架的语言就是动态类型的语言。</span></p><p><code>select 1</code><span> 表示连接是否成功，不返回任何数据。</span></p><p><img src="Node.js.assets/image-20220315123056034.png" referrerpolicy="no-referrer" alt="image-20220315123056034"></p><ul><li><span>插入数据</span></li></ul><p><span>插入数据和查询数据类似，只是带有了插入的对象，第一个参数还是sql语句，只是sql语句中，可以使用</span><code>？</code><span>进行占位 。使用？进行展位，这样能够动态的防御注入风险。</span></p><p><img src="Node.js.assets/image-20220315123833643.png" referrerpolicy="no-referrer" alt="image-20220315123833643"></p><p><span>简化写法</span></p><p><span>使用关键字和？进行数据的整体插入，便捷的插入。</span></p><p><img src="Node.js.assets/image-20220315141436212.png" referrerpolicy="no-referrer" alt="image-20220315141436212"></p><ul><li><span>更新数据</span></li></ul><p><img src="Node.js.assets/image-20220315141720275.png" referrerpolicy="no-referrer" alt="image-20220315141720275"></p><p><span>简化写法和插入数据一样，都是使用关键字和？进行简化数据，只需要数据传递的正确，那么就可以使用。如果直接写成一个？，那么模块会自动将传入的参数进行解构。</span></p><ul><li><span>删除数据</span></li></ul><p><img src="Node.js.assets/image-20220315142200132.png" referrerpolicy="no-referrer" alt="image-20220315142200132"></p><p><span>标记删除</span></p><p><img src="Node.js.assets/image-20220315142234806.png" referrerpolicy="no-referrer" alt="image-20220315142234806"></p><p>&nbsp;</p><p><span>js的模块会自动将数据进行解构。</span></p><p>&nbsp;</p><h1><a name="web开发模式" class="md-header-anchor"></a><span>Web开发模式</span></h1><p><img src="Node.js.assets/image-20220315142404190.png" referrerpolicy="no-referrer" alt="image-20220315142404190"></p><h2><a name="服务端的渲染" class="md-header-anchor"></a><span>服务端的渲染</span></h2><p><span>常用中间件</span></p><ul><li><span>mysql  提供数据库服务 @2.18.10</span></li><li><span>bcryptjs 提供密码加密服务 @2.4.3</span></li><li><span>express.json()  提供了解析post方法的json类型的body，通过 express.json() 这个中间件，解析表单中的 JSON 格式的数据</span></li><li><span>express.urlencoded() 提供了解析post方法的普通body xml/application类型，通过 express.urlencoded() 这个中间件，来解析表单中的 url-encoded 格式的数据</span></li><li><span>jsonwebtoken 提供了jwt的生成服务，记得定义密钥</span></li><li><span>express-jwt 提供了解密token的服务，记得得到密钥</span></li><li><span>multer 提供了处理form data 请求体的功能。</span></li></ul><p><img src="Node.js.assets/image-20220315142437614.png" referrerpolicy="no-referrer" alt="image-20220315142437614"></p><h3><a name="优势和劣势" class="md-header-anchor"></a><span>优势和劣势</span></h3><p><img src="Node.js.assets/image-20220315142546683.png" referrerpolicy="no-referrer" alt="image-20220315142546683"></p><h2><a name="前后端分离模式" class="md-header-anchor"></a><span>前后端分离模式</span></h2><p><span>依赖于Ajax的发展</span></p><p><img src="Node.js.assets/image-20220315142635224.png" referrerpolicy="no-referrer" alt="image-20220315142635224"></p><h3><a name="优势和劣势-n1040" class="md-header-anchor"></a><span>优势和劣势</span></h3><p><img src="Node.js.assets/image-20220315142707016.png" referrerpolicy="no-referrer" alt="image-20220315142707016"></p><p><span>对于企业来说最重要的是如何优化SEO，可以选择SSR技术，进行SEO优化。</span></p><h2><a name="如何选择开发模式" class="md-header-anchor"></a><span>如何选择开发模式</span></h2><p><img src="Node.js.assets/image-20220315142837871.png" referrerpolicy="no-referrer" alt="image-20220315142837871"></p><p><span>简而言之：如果不关注SEO优化并且交互效果很多，如后台登陆系统等，那么直接前后端分离比较好，如果关注SEO优化，如各种网站的首页等，那么使用服务端渲染比较好。 </span></p><h2><a name="前后端的身份认证" class="md-header-anchor"></a><span>前后端的身份认证</span></h2><p><img src="Node.js.assets/image-20220315142948013.png" referrerpolicy="no-referrer" alt="image-20220315142948013"></p><p>&nbsp;</p><h3><a name="原因" class="md-header-anchor"></a><span>原因</span></h3><p><img src="Node.js.assets/image-20220315143156191.png" referrerpolicy="no-referrer" alt="image-20220315143156191"></p><h3><a name="认证技术" class="md-header-anchor"></a><span>认证技术</span></h3><p><img src="Node.js.assets/image-20220315143212442.png" referrerpolicy="no-referrer" alt="image-20220315143212442"></p><p><span>现在前后端分离中的主要认证方式就是通过JWT技术进行认证。</span></p><h2><a name="http协议的无状态性" class="md-header-anchor"></a><span>HTTP协议的无状态性</span></h2><p><span> HTTP对于每一次的连接，任何两个连接之间都是没有直接的关系的</span></p><p><img src="Node.js.assets/image-20220315143424313.png" referrerpolicy="no-referrer" alt="image-20220315143424313"></p><p><span>所以需要证明需要的两个请求之间有关联，那么就需要使用到认证技术，也就是每次通过这些凭证来证明这两个连接之间是有关系的。</span></p><h3><a name="session认证" class="md-header-anchor"></a><span>Session认证</span></h3><p><img src="Node.js.assets/image-20220315143636496.png" referrerpolicy="no-referrer" alt="image-20220315143636496"></p><p><span>每当访问对应的网站时，浏览器会自动的自动发送对应的域名下的cookie，是域名独立的，不同的域名之间不会相互影响，在访问相应的网站时，会自动的，一次性的将所有的未过期的cookie发送到对应的网站上。</span><img src="Node.js.assets/image-20220315143841431.png" referrerpolicy="no-referrer" alt="image-20220315143841431"></p><h3><a name="不具有安全性" class="md-header-anchor"></a><span>不具有安全性</span></h3><p><img src="Node.js.assets/image-20220315144241747.png" referrerpolicy="no-referrer" alt="image-20220315144241747"></p><p><span>安全，隐私，敏感的数据一般不放在本地的浏览上</span></p><p><img src="Node.js.assets/image-20220315144350531.png" referrerpolicy="no-referrer" alt="image-20220315144350531"></p><p><span>解决方案  提高认证额安全性</span></p><p><img src="Node.js.assets/image-20220315144448297.png" referrerpolicy="no-referrer" alt="image-20220315144448297"></p><h3><a name="认证的原理" class="md-header-anchor"></a><span>认证的原理</span></h3><p><img src="Node.js.assets/image-20220315144537704.png" referrerpolicy="no-referrer" alt="image-20220315144537704"></p><p><span>在服务中存储登录的状态，而不是通过客户端记录状态，这样的控制权就掌握在服务器受伤，而不是客户端的手上。这样客户端无法伪造数据，提高了数据验证的安全性。</span></p><p>&nbsp;</p><h2><a name="jwt认证原理" class="md-header-anchor"></a><span>JWT认证原理</span></h2><p><img src="Node.js.assets/image-20220315144903294.png" referrerpolicy="no-referrer" alt="image-20220315144903294"></p><ul><li><span>在客户端将用户的数据发送给服务端</span></li><li><span>服务端根据用户的数据生成一个token</span></li><li><span>用户的本地数据中存储这个token</span></li><li><span>每次发送请求就会带上这个token，Authorizations的字段中。</span></li></ul><h3><a name="组成" class="md-header-anchor"></a><span>组成</span></h3><p><img src="Node.js.assets/image-20220315145119726.png" referrerpolicy="no-referrer" alt="image-20220315145119726"></p><p><span>组成部分的功能，header和signature都是为了保证payload的安全性，payload是真正存放用户数据的地方。</span></p><p><img src="Node.js.assets/image-20220315145256522.png" referrerpolicy="no-referrer" alt="image-20220315145256522"></p><h3><a name="使用-n1140" class="md-header-anchor"></a><span>使用</span></h3><p><img src="Node.js.assets/image-20220315145528762.png" referrerpolicy="no-referrer" alt="image-20220315145528762"></p><h3><a name="定义密钥" class="md-header-anchor"></a><span>定义密钥</span></h3><p><img src="Node.js.assets/image-20220315145917536.png" referrerpolicy="no-referrer" alt="image-20220315145917536"></p><h3><a name="生成token" class="md-header-anchor"></a><span>生成token</span></h3><p><img src="Node.js.assets/image-20220315150154626.png" referrerpolicy="no-referrer" alt="image-20220315150154626"></p><p>&nbsp;</p><h2><a name="验证" class="md-header-anchor"></a><span>验证</span></h2><p><span>详细使用文档 ： npmjs.org 中进行搜索即可</span></p><p><img src="Node.js.assets/image-20220315205849349.png" referrerpolicy="no-referrer" alt="image-20220315205849349"></p><p>&nbsp;</p><p><span>参数中可以添加中间件，即中间件的调用过程中，还可以要求必须要另外i一个中间件完成了验证返回才允许执行</span></p><p><img src="Node.js.assets/image-20220315210451464.png" referrerpolicy="no-referrer" alt="image-20220315210451464"></p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p><span>使用express的流程：</span></p><ul><li><span>首先明确路由模块，每一个不同的服务对象都是一个不同的模块，需要拆分</span></li><li><span>定义路由函数，将路由函数和路由进行拆分</span></li><li><span>定义验证规则，将逻辑处理和验证规则进行拆分，方便以后的维护</span></li><li><span>最后返回信息</span></li></ul></div>
<script src="./nav.js"></script>
</body>
</html>